﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using Silvermoon.Controls;
using Passwords.Models;
using Silvermoon.Windows.Styles;
using Passwords.Images;
using System.Drawing;
using Silvermoon.Shapes;
using Silvermoon.Windows;
using Passwords.Utils;
using Silvermoon.OpenGL;
using Passwords.Pages;
using Silvermoon.Navigation;
using Silvermoon.UI;
using Silvermoon.Controls.Base;
using Silvermoon.Animations;
using Passwords.Biz;

namespace Passwords.Controllers
{
    public static class PasswordsControllerHelper
    {

        public static readonly Color HighlightColor = PhoneColors.Orange;

        public static string TextToHighlight = "";
        public static PasswordsPage PasswordsPage;

        public static ListViewController<TitleDetailsImageItem, Password> InitializeAllPasswordsController(ListView listView)
        {
            var controller = InitializePasswordController(listView);
            SetControllerAzIndex(controller);
            controller.DataSource = Context.Current.Passwords.ToList();
            return controller;
        }

        private static string azIndex = "#ABCDEFGHIJKLMNOPQRSTUVWXYZ";

        public static void SetControllerAzIndex(ListViewController<TitleDetailsImageItem, Password> controller)
        {
            controller.GroupDescriptors.Clear();
            foreach (char c in azIndex)
            {
                controller.GroupDescriptors.Add(new ItemGroupDescriptor<Password>(c.ToString()));
                controller.GetDescriptorFromValue += new EventHandler<GroupDescriptorEventArgs<Password>>(controller_GetDescriptorFromValue);
            }

        }

        static void controller_GetDescriptorFromValue(object sender, GroupDescriptorEventArgs<Password> e)
        {
            string s = (e.Value as Password).Name;
            char c = s.Length > 0 ? char.ToUpper(s[0]) : char.MinValue;
            int index = Math.Max(0, azIndex.IndexOf(c));
            e.Descriptor = e.GroupDescriptors[index];
        }

        public static ListViewController<TitleDetailsImageItem, Password> InitializeFavesController(ListView favesView)
        {
            var controller = InitializePasswordController(favesView);
            controller.DataSource = Context.Current.Favorites.ToList();
            return controller;
        }


        private static ListViewController<TitleDetailsImageItem, Password> InitializePasswordController(ListView listView)
        {
            var controller = new ListViewController<TitleDetailsImageItem, Password>(listView);
            //SetStyles(controller);
            SetMinStyles(controller);
            controller.UpdateItem += new EventHandler<ItemControlGeneratorEventArgs<TitleDetailsImageItem>>(UpdatePasswordItem);
            controller.InitializeItem += new EventHandler<ItemControlGeneratorEventArgs<TitleDetailsImageItem>>(InitializeItem);
            return controller;
        }

        static void InitializeItem(object sender, ItemControlGeneratorEventArgs<TitleDetailsImageItem> e)
        {
            e.Item.HighlightTextColor = HighlightColor;
            e.Item.HighlightMode = TDIHighlightMode.Title;
            e.Item.CommandRaised += (s, ee) =>
                {
                    Password password = (s as Item).DataValue as Password;
                    AppController.CurrentPassword = password;
                    ee.Object = password;
                };
        }

        static void InitializeCategoryPasswordItem(object sender, ItemControlGeneratorEventArgs<TitleDetailsImageItem> e)
        {
            e.Item.HighlightTextColor = HighlightColor;
            e.Item.HighlightMode = TDIHighlightMode.Title;
            e.Item.CommandRaised += (s, args) =>
                {
                    Password password = ((s as Item).DataValue as CategorizedPassword).Password;
                    AppController.CurrentPassword = password;
                    args.Object = password;
                };
        }


        public static ListViewController<TitleDetailsImageItem, CategorizedPassword> InitializeCategoryPasswordsController(ListView listView)
        {
            var controller = new ListViewController<TitleDetailsImageItem, CategorizedPassword>(listView);
            SetStyles(controller);
            AddCategoryDescriptors(controller);
            controller.UpdateItem += new EventHandler<ItemControlGeneratorEventArgs<TitleDetailsImageItem>>(UpdateCategoryPasswordItem);
            controller.InitializeItem += new EventHandler<ItemControlGeneratorEventArgs<TitleDetailsImageItem>>(InitializeCategoryPasswordItem);

            controller.GetDescriptorFromValue += new EventHandler<GroupDescriptorEventArgs<CategorizedPassword>>(GetCategoryDescriptor);
            controller.DataSource = GetPasswordItems();
            return controller;
        }



        private static IEnumerable<CategorizedPassword> GetPasswordItems()
        {
            return Context.Current.GetCategorizedPasswords();
        }

        private static void AddCategoryDescriptors(ListViewController<TitleDetailsImageItem, CategorizedPassword> controller)
        {
            var descriptors = controller.GroupDescriptors;
            foreach (Category c in Context.Current.Categories)
            {
                descriptors.Add(new ItemGroupDescriptor<CategorizedPassword>(c.Name));
            }
        }


        static void GetCategoryDescriptor(object sender, GroupDescriptorEventArgs<CategorizedPassword> e)
        {
            int index = Context.Current.Categories.IndexOf(e.Value.Category);
            e.Descriptor = e.GroupDescriptors[index];
        }

        private static void SetStyles(ListViewControllerBase<TitleDetailsImageItem> controller)
        {
            controller.PickerTileSize = new Size(400, 80);
            controller.GroupButtonSize = new Size(400, 80);
            controller.GroupPickerStyle = new TextItemStyle
            {
                Padding = Margin.Centered,
                Font = FontCache.BigRegularFont,
            };

            controller.GroupButtonStyle = new ButtonStyle
            {
                Margin = Margin.Zero,
                Background = Shape.Empty,
                TextColor = PhoneColors.Accent,
                Font = FontCache.BigBoldFont,
                Padding = new Margin(0, null, null, 8),
                Chrome = Shape.Empty
            };
            SetMinStyles(controller);
        }

        private static void SetMinStyles(ListViewControllerBase<TitleDetailsImageItem> controller)
        {
            controller.ItemHeight = 100;
            controller.ItemStyle = new Style { Margin = new Margin(10, 0, ScrollViewer.ScrollbarWidth + 2, 0) };
        }

        static void UpdateCategoryPasswordItem(object sender, ItemControlGeneratorEventArgs<TitleDetailsImageItem> e)
        {
            var item = e.Item;
            CategorizedPassword password = e.Value as CategorizedPassword;
            item.ContextMenu = PasswordsPage.PasswordItemContextMenu;
            item.ShowContextMenu += (ss, ee) =>
            {
                CategorizedPassword cp = (ss as Item).DataValue as CategorizedPassword;
                PasswordsPage.PasswordItemContextMenu.IsFavesEnabled = !cp.Password.IsFavorite;
            };
            UpdateItem(item, password.Password);

        }


        static void UpdatePasswordItem(object sender, ItemControlGeneratorEventArgs<TitleDetailsImageItem> e)
        {
            var item = e.Item;
            Password password = e.Value as Password;
            item.ContextMenu = PasswordsPage.PasswordItemContextMenu;
            item.ShowContextMenu += (ss, ee) =>
            {
                Password pw = (ss as Item).DataValue as Password;
                PasswordsPage.PasswordItemContextMenu.IsFavesEnabled = !pw.IsFavorite;
            };
            UpdateItem(item, password);
        }

        private static void UpdateItem(TitleDetailsImageItem item, Password password)
        {
            item.Title = password.Name;
            item.Image = password.IsFavorite ? ImagePool.FaveItem : ImagePool.Lock;
            item.Command = PasswordCommands.Password;
            //item.Command = PasswordCommands.DeletePassword;  // for testing only
            item.Details = GetPasswordDetails(password);
            item.Highlight(TextToHighlight);
        }

        private static string GetPasswordDetails(Password password)
        {
            if (password == null) return string.Empty;

            string details = (from item in password.Items where !item.IsPassword select item.Value).FirstOrDefault();
            if (details == null) details = string.Empty;
            return details;
        }




    }
}
